// pages/productDetails/productDetails.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    numbers: 1,
    modifyPriceCol: true,
    share:1,
    share_id:'',
    cartCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.hideShareMenu() // 隐藏分享按钮

    var that = this;
    wx.getUserInfo({
      success: function (res) {
        that.setData({
          userInfo: res.userInfo,
          avatar: res.userInfo.avatarUrl
        })
      }
    })

    if (options.sku_code) {
      this.setData({
        sku_code: options.sku_code
      })
    }

    if (options.uid) {
      this.setData({
        uid: options.uid
      })
    }

    if (options.share_id) {
      this.setData({
        share_id: options.share_id
      })
    }

    if (options.q) {
      var link = options.q;
      link = unescape(decodeURI(link));
       var num = link.indexOf("?");
       link = link.substring(num+1);
       var ary = link.split("&");
       var obj = {}
       for (var i = 0; i < ary.length;i++){
        var key = ary[i].split("=")[0];
        var value = ary[i].split("=")[1];
        obj[key] = value
       }
       this.setData(obj)
    }


    this.getProductInfo();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      user_flag: wx.getStorageSync('accountInfo').user_flag
    })
    this.getProductInfo();
    this.cartNum();
  },

  getProductInfo: function() {
    var that = this;
    var params = new Object()
    params.share_id = this.data.share_id;
    if (this.data.shop_id) {
      params.shop_id = this.data.shop_id;
    }
    
    util.showLoading("数据加载中..");
    api.POST({
      params: params,
      url: app.globalData.base_url + 'goods/shareInfo',
      success: function(res) {
        util.hiddenLoading();
        if (util.isEmpty(res.data.code)) {
          that.setData({
            rawData: res.data.data,
            newPrice: res.data.data.current_price
          });
        }

      },
      fail: function(fail) {
        util.showErrorPath("获取失败，请检查网络");
      }
    })
  },
  addNum: function() {
    var num = this.data.numbers;
    num++;
    this.setData({
      numbers: num
    })
  },
  minusNum: function() {
    var num = this.data.numbers;
    if (num > 1) {
      num--;
    }
    this.setData({
      numbers: num
    })
  },
  // 加入购物车
  addCart: function() {
    var that = this;
    var params = new Object()
    params.sku_code = this.data.sku_code;
    params.num = this.data.numbers;
    if (this.data.share_id) {
      params.share_id = this.data.share_id;
    }
    api.POST({
      params: params,
      url: app.globalData.base_url + 'shoppingcart/addGoods',
      success: function(res) {
        if (util.isEmpty(res.data.code)) {
          util.showSuccess("加入成功");
        } else {
          util.showStatus(res.data.message)
        }

      },
      fail: function(fail) {
        util.showErrorPath("获取失败，请检查网络");
      }
    })
  },
  // 立即购买
  pay: function() {
    var orderList = [];
    var box = new Object();
    box.num = this.data.numbers;
    box.stock = this.data.rawData.stock;
    // if (this.data.shop_id) {
    //   box.shop_id = this.data.shop_id;
    //   var shop_id = this.data.shop_id;
    //   if (box.num > box.stock) {
    //     util.showStatus("库存不足");
    //     return;
    //   }
    // }
    if (this.data.share_id) {
      var share_id = this.data.share_id;
    }
    if (this.data.uid) {
      var uid = this.data.uid;
    } else {
      var uid = ''
    }

    box.sku_code = this.data.sku_code;
    box.current_price = this.data.rawData.share_price;
    box.gname = this.data.rawData.gname;
    box.image = this.data.rawData.image;
    box.degree = this.data.rawData.degree;
    box.degree_unit = this.data.rawData.degree_unit;
    box.volume = this.data.rawData.volume;
    box.volume_unit = this.data.rawData.volume_unit;
    box.total = (this.data.numbers * this.data.rawData.share_price).toFixed(2);
    orderList.push(box);
    wx.setStorageSync('orderList', orderList);
    if (share_id) {
      wx.navigateTo({
        url: '/index/pages/order/order?share_id=' + share_id
      })
    }else {
      wx.navigateTo({
        url: '/index/pages/order/order'
      })
    }
  },
  linkCart: function () {
    wx.navigateTo({
      url: '/index/pages/cart/cart'
    })
  },
  onShareAppMessage: function(res) {
    var that = this;
    var uid = '&uid=' + wx.getStorageSync('accountInfo').uid;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: that.data.rawData.gname,
        path: '/index/pages/productDetails/productDetails?sku_code=' + that.data.sku_code + uid,
        imageUrl: that.data.rawData.share_img,
        success: function(res) {
          // 转发成功
        },
        fail: function(res) {
          // 转发失败
        }
      }
    }
  },
  modifyPrice: function() {
    this.setData({
      modifyPriceCol: false
    })
  },
  closeModifyPrice: function() {
    this.setData({
      modifyPriceCol: true
    })
  },
  newPrice: function(e) {
    var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    var money = e.detail.value/1
    var newPrice = this.data.newPrice/1
    var current_price = this.data.rawData.current_price / 1
    var minprice = this.data.rawData.share_bottom_price/1
    if (reg.test(money)) {
      if (money < minprice){
        money = minprice
      } else if (money > current_price){
        money = current_price
      }
      money = money.toFixed(2);
    } else {
      money = this.data.newPrice
    }
    this.setData({
      newPrice: money
    })
  },
  //购物车商品数量
  cartNum: function () {
    var that = this;
    api.POST({
      params: '',
      url: app.globalData.base_url + 'shoppingcart/countGoods',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          that.setData({
            cartCount: res.data.data.countNum
          })
        }

      },
      fail: function (fail) {
        util.showErrorPath("获取失败，请检查网络");
      }
    })
  },
  //确认修改价格
  // submitModifyPrice: function () {
  //   var that = this;
  //   var params = new Object()
  //   params.sku_code = this.data.rawData.sku_code;
  //   params.share_price = this.data.newPrice;
  //   api.POST({
  //     params: params,
  //     url: app.globalData.base_url + 'goods/shareSub',
  //     success: function (res) {
  //       if (util.isEmpty(res.data.code)) {
  //         util.showStatus("修改成功，请点击分享")
  //         that.setData({
  //           share:2,
  //           share_id: res.data.data.share_id
  //         })
  //       } else {
  //         util.showStatus(res.data.message)
  //       }

  //     },
  //     fail: function (fail) {
  //       util.showErrorPath("../../../", "获取失败，请检查网络");
  //     }
  //   })
  // }

})